package com.model.controller;

import com.model.service.UserService;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.logging.Handler;

public class MyInter implements HandlerInterceptor {
    @Resource
    UserService userService;
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        if(session.getAttribute("Login")==null){
            String username = request.getParameter("username");
            if(username!=null&&!"".equals(username)){
                String password = request.getParameter("password");
                if(password!=null&&!"".equals(password)){
                    String realPassword = userService.listByUsername(username);
                    if(realPassword!=null&&password.equals(realPassword)){
                        session.setAttribute("Login",true);
                        session.setAttribute("name",userService.listName(username));
                        return true;
                    } else{
                        String handlerPath = ((HandlerMethod)handler).getMethodAnnotation(RequestMapping.class).path()[0];
                        request.setAttribute("handlerPath",handlerPath);
                        request.setAttribute("errorMsg","用户名或密码错误");
                        RequestDispatcher requestDispatcher = request.getRequestDispatcher("/login.jsp");
                        requestDispatcher.forward(request,response);
                        return false;
                    }
                }else{
                    String handlerPath = ((HandlerMethod)handler).getMethodAnnotation(RequestMapping.class).path()[0];
                    request.setAttribute("handlerPath",handlerPath);
                    request.setAttribute("errorMsg","未输入密码");
                    RequestDispatcher requestDispatcher = request.getRequestDispatcher("/login.jsp");
                    requestDispatcher.forward(request,response);
                    return false;
                }
            }
            else{
                String handlerPath = ((HandlerMethod)handler).getMethodAnnotation(RequestMapping.class).path()[0];
                request.setAttribute("handlerPath",handlerPath);
                RequestDispatcher requestDispatcher = request.getRequestDispatcher("/login.jsp");
                requestDispatcher.forward(request,response);
                return false;
            }
        }else{
            return true;
        }
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }
}
